<?php
class Meetings extends CActiveRecord {
	/**
	 * The followings are the available columns in table 'meetings':
	 * @var integer $id
	 * @var string $meeting_date
	 * @var integer $project_id
	 * @var integer $meeting_comment
	 * @var string $next_date
	 * @var integer $completed
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'meetings';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		return array (
			array (
				'meeting_date',
				'required'
			),
			array (
				'completed',
				'numerical',
				'integerOnly' => true
			)
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array (
			'project' => array (
				self::BELONGS_TO,
				'Project',
				'project_id'
			)
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array (
			'id' => Yii::t('database', 'Id'),
			'meeting_date' => Yii::t('database', 'Meeting Date'),
			'project_id' => Yii::t('database', 'Project'),
			'meeting_comment' => Yii::t('database', 'Meeting Comment'),
			'next_date' => Yii::t('database', 'Next Date'),
			'completed' => Yii::t('database', 'Completed')
		);
	}

	private $_objectives = null;
	private $_previous_objectives = null;
	private $_steps = null;

	public function getPreviousObjectives() {
		if ($this->_objectives == null) {
			$sql =<<<SQL
select a.* 
from  project_topic a 
where a.project_id=:project_id 
and   a.objective = 'y'
and   a.objective_date < :meeting_date 
and   a.objective_date is not null
and   (
           a.adviser_validation_date is null 
        or a.adviser_validation_date >= :meeting_date
)
SQL;
			$this->_previous_objectives = ProjectTopic::model()->findAllBySql($sql, array (
				':project_id' => $this->project_id,
				':meeting_date' => $this->meeting_date
			));
			if ($this->_previous_objectives == null)
				$this->_previous_objectives = array ();
		}

		return $this->_previous_objectives;
	}

	public function getObjectives() {
		if ($this->_objectives == null) {
			$sql =<<<SQL
select a.* 
from  project_topic a 
where a.project_id=:project_id 
and   a.objective = 1 
and   a.objective_date <= :meeting_date 
and   a.objective_date is not null
and   (
           a.adviser_validation_date is null 
        or a.adviser_validation_date > :meeting_date
      )
SQL;
			$this->_objectives = ProjectTopic::model()->findAllBySql($sql, array (
				':project_id' => $this->project_id,
				':meeting_date' => $this->meeting_date
			));
			if ($this->_objectives == null)
				$this->_objectives = array ();
		}

		return $this->_objectives;
	}

	public function steps() {
		if ($this->_steps == null) {
			$this->_steps = ProjectSelectedSteps::model()->findAll('project_id=:project_id', array (
				':project_id' => $this->project->id
			));
			if ($this->_steps == null)
				$this->_steps = array ();
		}

		return $this->_steps;
	}
	
	
	function getMeeting_date_display()
	{
		return Constants::fromSqlDate($this->meeting_date);
	}
	
	private $_validated_topics = null;
	public function getvalidated_topics()
	{
		if ($this->_validated_topics == null) {
			$sql =<<<SQL
select a.* 
from  project_topic a 
where a.project_id=:project_id 
and   a.adviser_validation_date = :meeting_date
SQL;
			$this->_validated_topics = ProjectTopic::model()->findAllBySql($sql, array (
				':project_id' => $this->project_id,
				':meeting_date' => $this->meeting_date
			));
			if ($this->_validated_topics == null)
				$this->_validated_topics = array ();
		}

		return $this->_validated_topics;
	}
	
	public function beforeSave() {
		$b = parent::beforeSave();
		
		if ($this->next_date != null && $this->next_date instanceof Date) {
			$this->next_date = $this->next_date->toMysqlDateTimeString();
		}
		
		return $b;
	}
	
	
	public function afterFind() {
		$b = parent::afterFind();
		
		if ($this->next_date == '0000-00-00') {
			$this->next_date = null;
		}
		
		if ($this->next_date != null) {
			$this->next_date = new Date(strtotime($this->next_date));
		}
		
		return $b;
	}
	
	public function afterSave() {
		parent::afterSave();
		
		if ($this->next_date == '0000-00-00') {
			$this->next_date = null;
		}
		
		if ($this->next_date != null) {
			$this->next_date = new Date(strtotime($this->next_date));
		}
	}
}
?>